#include<iostream>
#include<event2/listener.h>
#include<event2/bufferevent.h>
#include<event2/buffer.h>
#include<arpa/inet.h>

#define PORT 8403
using namespace std;

void on_read_cb(struct bufferevent *bev,void* ctx){
	struct evbuffer *input=bufferevent_get_input(bev);
	struct evbuffer *output=bufferevent_get_output(bev);
	evbuffer_add_buffer(output,input);
}

void on_accept_cb(struct evconnlistener *listener,evutil_socket_t accept_fd,struct sockaddr* addr,int socklen,void* ctx){
	struct event_base *base=evconnlistener_get_base(listener);
	struct bufferevent *bev=bufferevent_socket_new(base,accept_fd,0);//a socket correspond a bufferevent
	bufferevent_enable(bev,EV_READ|EV_WRITE);
	bufferevent_setcb(bev,on_read_cb,NULL,NULL,NULL);
}//when a client connection is comming,callback function is triggered

int main(){
	struct sockaddr_in serveraddr;
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=PORT;
	serveraddr.sin_addr.s_addr=INADDR_ANY;//listen ports on every network card

	struct event_base *base=event_base_new();
	
	struct evconnlistener* listener=evconnlistener_new_bind(base,
				on_accept_cb,
				NULL,
				LEV_OPT_REUSEABLE,
				10,
				(struct sockaddr*)&serveraddr,
				sizeof(serveraddr));//create socket ,bind socket, listen socket 	
	
	event_base_dispatch(base);	

	return 0;	
}
